package com.qf.cloud2203.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class TimeFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        final long start = System.currentTimeMillis();


        return chain.filter(exchange).then(Mono.fromRunnable(new Runnable() {
            @Override
            public void run() {
                final long end = System.currentTimeMillis();

                System.out.println(exchange.getRequest().getPath()+"cost time:"+(end-start)+"ms");
            }
        }));

    }

    @Override
    public int getOrder() {
        return -100;
    }
}
